<template>
  <div>
    <!-- 权限点数据展示 -->
    <el-tree
      ref="tree"
      :data="permissionData"
      :props="{ label: 'name' }"
      default-expand-all
      show-checkbox
      node-key="id"
      check-strictly
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button>取消</el-button>
    </div>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  name: '',
  props: {
    roleId: {
      type: String,
      required: true
    },
    show: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      permIds: [],
      permissionData: []// 收集权限数据
    }
  },
  computed: {
  },
  created() {
    this.loadPermissionList()
    this.loadPermissionByRoleId()
  },
  methods: {
    // 封装函数用于发送请求, 获取权限列表
    async loadPermissionList() {
      const { data } = await getPermissionList()
      //   console.log('权限列表的数据是', data.data)
      this.permissionData = tranListToTreeData(data.data, '0')
    //   console.log(this.permissionData)
    },
    // 获取角色详细
    async loadPermissionByRoleId() {
    // 根据roleId获取当前这个角色已经具备的权限
      const { data } = await getRoleDetail(this.roleId)
      console.log('获取当前角色的已有的权限点数据', data.data.permIds)
      this.permIds = data.data.permIds
      this.$refs.tree.setCheckedKeys(this.permIds)
    },
    // 设置权限确认保存
    async  hSave() {
      // 看Element 获取权限id集合的方法
      this.permIds = this.$refs.tree.getCheckedKeys()
      await assignPerm({ id: this.roleId, permIds: this.permIds })
      // 关闭对话框
      this.$emit('update:show', false)
    }
  }
}
</script>
<style lang='less'  scoped>

</style>
